﻿Ext.extend(demo.module,{
    init: function(){
      var pageParams={start:0,limit:20};
 	  var opt={ 
	  url:'categoryquery'	  
	  };  	
	  Ext.reg("combobox",Ext.form.ComboBox);
	  var productChannel={name:'channelid',text:'栏目',ft:{
	    xtype:'combobox',
	    store: new Ext.data.Store({
	            proxy: new Ext.data.HttpProxy({
	                method: 'GET',
	                url:"productchannelquery?limit=100"
	            }),
	            reader: new Ext.data.JsonReader({
	                root: 'root',	              
	                fields:["id","name"] 
	            })		    
		}),
		hiddenName:'channelid',
		editable:false,
	    displayField:'name',
	    valueField:'id',
	    allowBlank:false,
	    typeAhead: true,	
	    triggerAction: 'all',
	    emptyText:'请选择产品栏目',
	    selectOnFocus:true
	    },
	    header:{renderer:function(v,m,r){
	     return r.get("channel").name;
	    }}
	  };
	  opt.fields=[
	  {name:'id',text:'id',header:{hidden:true},ft:'hidden'},
      productChannel,	  
	  {name:'channel',isdata:true,header:{hidden:true}},
	  {name:'name',text:'分类名称',ft:{allowBlank:false}},	  
	  {name:'seq',text:'序列',ft:{xtype:'numberfield',allowBlank:false,allowNegative:false,allowDecimals:false}}	
	  ];	  
	  opt.gridConfig={};	
	  opt.gridConfig.tbar=[
	   {text:'添加',iconCls:'add',handler:function(x){
	     this.window.setTitle(x);
	     this.window.show();
	   }},
	   {
	     text:'修改',iconCls:'edit',handler:function(x)
	     {
	       this.window.setTitle(x);
	       var Record=Ext.data.Record.create([
		         {name: 'id', mapping: 'id'},
		         {name: 'name', mapping: 'name'}		         
		       ]);
		   var cr=this.getSelection();  
		   var r=new Record(cr.get("channel"));   
	       this.edit();
	       var combox=this.form.getForm().findField("channelid");
	       combox.getStore().add(r);
	       combox.setValue(cr.get("channelid"));

	     }
	   },
	   {
	     text:'删除',iconCls:'delete',handler:function(x)
	     {	       
	       this.del(function(rs){
	         this.postJson({
	           url:'deletecategory',
	           params:{category:{id:rs[0].get("id")}}
	         })
	       })
	     }
	   
	   },
	   '->',
	   '栏目:',
	   Ext.apply(Ext.apply({},productChannel.ft),{
	    xtype:'clearablecombobox',
	    listeners:{
	      select :function(t,r){
	         g.grid.getStore().baseParams["category.channelid"]=r.get("id");
	         g.grid.getStore().reload();
	      },
	      afterclear:function()
	      {
	         g.grid.getStore().baseParams["category.channelid"]='';
	         g.grid.getStore().reload();	        
	      } 
	    },	   
	    allowBlank:true
	   })	   
	  ];  // end tbar
	  
	  
     opt.gridConfig.bbar={pageSize:pageParams.limit};     
	 var g=new Ext.fay.ui.CURDGrid(opt);
	 g.on("save",function(values){
	    if(this.isValid())
	    {
	      this.postJson({
	       url:'savecategory',
	       params:{category:values}
	      })
	    }
	 });  
	 this.main.add(g.grid);
     this.main.doLayout();   
      g.ds.reload({params:pageParams});
   }
});